package com.ruoyi.web.controller.job;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.job.domain.JobInfo;
import com.ruoyi.job.service.IJobInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 兼职详情Controller
 *
 * @author ace
 * @date 2020-02-02
 */
@Controller
@RequestMapping("/job/info")
public class JobInfoController extends BaseController {
    private String prefix = "job/info";

    @Autowired
    private IJobInfoService jobInfoService;

    @RequiresPermissions("job:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询兼职详情列表
     */
    @RequiresPermissions("job:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JobInfo jobInfo) {
        startPage();
        Long userId = ShiroUtils.getSysUser().getUserId();
        jobInfo.setCreatedBy(userId);
        List<JobInfo> list = jobInfoService.selectJobInfoList(jobInfo);
        return getDataTable(list);
    }

    /**
     * 导出兼职详情列表
     */
    @RequiresPermissions("job:info:export")
    @Log(title = "兼职详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JobInfo jobInfo) {
        List<JobInfo> list = jobInfoService.selectJobInfoList(jobInfo);
        ExcelUtil<JobInfo> util = new ExcelUtil<JobInfo>(JobInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 新增兼职详情
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存兼职详情
     */
    @RequiresPermissions("job:info:add")
    @Log(title = "兼职详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JobInfo jobInfo) {
        Long userId = ShiroUtils.getSysUser().getUserId();
        jobInfo.setCreatedBy(userId);
        jobInfo.setCreatedTime(System.currentTimeMillis());
        return toAjax(jobInfoService.insertJobInfo(jobInfo));
    }

    /**
     * 修改兼职详情
     */
    @GetMapping("/edit/{jobInfoId}")
    public String edit(@PathVariable("jobInfoId") Long jobInfoId, ModelMap mmap) {
        JobInfo jobInfo = jobInfoService.selectJobInfoById(jobInfoId);
        mmap.put("jobInfo", jobInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存兼职详情
     */
    @RequiresPermissions("job:info:edit")
    @Log(title = "兼职详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JobInfo jobInfo) {
        Long userId = ShiroUtils.getSysUser().getUserId();
        jobInfo.setUpdatedBy(userId);
        jobInfo.setUpdatedTime(System.currentTimeMillis());
        return toAjax(jobInfoService.updateJobInfo(jobInfo));
    }

    /**
     * 删除兼职详情
     */
    @RequiresPermissions("job:info:remove")
    @Log(title = "兼职详情", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(jobInfoService.deleteJobInfoByIds(ids));
    }
}
